import {Clazz} from "./Clazz";
import * as THREE from 'three';

import Stats from 'three/examples/jsm/libs/stats.module.js';

import {FirstPersonControls} from 'three/examples/jsm/controls/FirstPersonControls.js';
import {ImprovedNoise} from 'three/examples/jsm/math/ImprovedNoise.js';
import * as BufferGeometryUtils from 'three/examples/jsm/utils/BufferGeometryUtils.js';

class GrassBlock extends Clazz {
    constructor(scene) {
        super();
        const geometries = [];
        const matrix = new THREE.Matrix4();
        matrix.setPosition(200,0,0);
        const pxGeometry = new THREE.PlaneGeometry( 100, 100 );
        pxGeometry.attributes.uv.array[ 1 ] = 0.5;
        pxGeometry.attributes.uv.array[ 3 ] = 0.5;
        pxGeometry.rotateY( Math.PI / 2 );
        pxGeometry.translate( 50, 0, 0 );

        const nxGeometry = new THREE.PlaneGeometry( 100, 100 );
        nxGeometry.attributes.uv.array[ 1 ] = 0.5;
        nxGeometry.attributes.uv.array[ 3 ] = 0.5;
        nxGeometry.rotateY( - Math.PI / 2 );
        nxGeometry.translate( - 50, 0, 0 );

        const pyGeometry = new THREE.PlaneGeometry( 100, 100 );
        pyGeometry.attributes.uv.array[ 5 ] = 0.5;
        pyGeometry.attributes.uv.array[ 7 ] = 0.5;
        pyGeometry.rotateX( - Math.PI / 2 );
        pyGeometry.translate( 0, 50, 0 );

        const pzGeometry = new THREE.PlaneGeometry( 100, 100 );
        pzGeometry.attributes.uv.array[ 1 ] = 0.5;
        pzGeometry.attributes.uv.array[ 3 ] = 0.5;
        pzGeometry.translate( 0, 0, 50 );

        const nzGeometry = new THREE.PlaneGeometry( 100, 100 );
        nzGeometry.attributes.uv.array[ 1 ] = 0.5;
        nzGeometry.attributes.uv.array[ 3 ] = 0.5;
        nzGeometry.rotateY( Math.PI );
        nzGeometry.translate( 0, 0, - 50 );

        geometries.push(pxGeometry.clone().applyMatrix4(matrix));
        geometries.push(nxGeometry.clone().applyMatrix4(matrix));
        geometries.push(pyGeometry.clone().applyMatrix4(matrix));
        geometries.push(pzGeometry.clone().applyMatrix4(matrix));
        geometries.push(nzGeometry.clone().applyMatrix4(matrix));

        const geometry = BufferGeometryUtils.mergeGeometries(geometries);
        geometry.computeBoundingSphere();

        const texture = new THREE.TextureLoader().load('../js/components/images/grassblock/atlas.png');
        texture.colorSpace = THREE.SRGBColorSpace;
        texture.magFilter = THREE.NearestFilter;

        const mesh = new THREE.Mesh(geometry, new THREE.MeshLambertMaterial({map: texture, side: THREE.DoubleSide}));
        scene.add(mesh);
    }
    update() {
        super.update();
    }
}

export {GrassBlock}
